<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<title>对props进行限制</title>
</head>

<body>
	<!-- 准备好一个“容器” -->
	<div id="test1"></div>
	<div id="test2"></div>
	<div id="test3"></div>

	<!-- 引入react核心库(React) -->
	<script type="text/javascript" src="../js/react.development.js"></script>
	<!-- 引入react-dom，用于支持react操作DOM(ReactDOM) -->
	<script type="text/javascript" src="../js/react-dom.development.js"></script>
	<!-- 引入babel，用于将jsx转为js -->
	<script type="text/javascript" src="../js/babel.min.js"></script>
	<!-- 引入prop-types，用于对组件标签属性props进行限制(PropTypes) -->
	<script type="text/javascript" src="../js/prop-types.js"></script>

	<script type="text/babel">
		//1.创建组件
		class Person extends React.Component {
			render() {
				const { name, age, sex } = this.props
				// props身上的属性都是只读的,下面一行代码修改只读属性报错
				// this.props.name = 'jack'
				return (
					<ul>
						<li>姓名：{name}</li>
						<li>性别：{sex}</li>
						<li>年龄：{age}</li>
					</ul>
				)
			}
		}

		// 对标签属性进行类型,必要性的限制
		Person.propTypes = {
			name: PropTypes.string.isRequired, //name,字符串必填
			sex: PropTypes.string, //sex字符串,默认值为男
			age: PropTypes.number, //age数字类型,默认值为18
			speak: PropTypes.func //speak函数类型
		}
		// 指定默认标签属性值
		Person.defaultProps = {
			sex: '男',
			age: 18
		}

		//2.渲染组件到页面
		ReactDOM.render(<Person name="tom" sex="男" age={18} speak={speak} />, document.getElementById('test1'))
		ReactDOM.render(<Person name="jerry" sex="女" age={19} />, document.getElementById('test2'))
		const p = { name: "赵思琪" }
		ReactDOM.render(<Person {...p} />, document.querySelector('#test3'))

		function speak() {
			console.log('我说话了');
		}
	</script>
</body>

</html>